package view.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import model.Constants;
import model.HostAddress;
import model.IPAddress;
import model.Subnet;
import view.components.HostComboBox;
import view.components.IPTextArea;
import view.dialogs.AddSubnetDialog;
import view.dialogs.InfoDialog;
import view.views.dialogContentViews.SubnetBoxPanel;
import controller.Calculator;
import controller.Controller;

public class AddSubnetListener implements ActionListener {

	private final AddSubnetDialog addSubnetDialog;
	private final SubnetBoxPanel subnetBoxPanel;
	private final HostComboBox hostBox;
	private final IPTextArea ipTextArea;

	public AddSubnetListener(AddSubnetDialog addSubnetDialog,
			SubnetBoxPanel subnetBoxPanel, HostComboBox hostBox) {
		this.addSubnetDialog = addSubnetDialog;
		this.subnetBoxPanel = subnetBoxPanel;
		this.hostBox = hostBox;
		this.ipTextArea = subnetBoxPanel.getIpText();

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String ip = Constants.EMPTY, s = Constants.EMPTY;
		IPAddress address;

		if (!hostBox.getBox().getSelectedItem().equals(Constants.EMPTY)) {

			if (!subnetBoxPanel.getSizeField().getText().isEmpty()
					|| !subnetBoxPanel.getSuffixField().getText().isEmpty()) {
				
				ArrayList<HostAddress> hostList = Controller.getInstance()
						.getHosts();
				int index = 0;
				for (HostAddress element : Controller.getInstance().getHosts()) {
					if (element.getHostAddress().getAddress()
							.equals(hostBox.getBox().getSelectedItem())) {
						index = hostBox.getBox().getSelectedIndex() - 1;

					}
				}


					ip = Calculator.getInstance().getIpFromIpTextArea(
							ipTextArea);

					s = subnetBoxPanel.getNameText().getText();

					if (s.equals(Constants.EMPTY) || s == null) {
						s = ip;
					}

					address = new IPAddress(ip);

					Subnet sub = checkFields(address, s);

					if (sub != null) {

					if (hostList.get(index).getRange() - sub.getSubnetSize() >= 0) {
						hostList.get(index).setRange(
								hostList.get(index).getRange()
								- (int) sub.getSubnetSize());
						hostList.get(index)
								.getSpaces()
								.add(Integer.parseInt(subnetBoxPanel
										.getSuffixField().getText()));
						hostList.get(index).getSubnetList().add(sub);
						Controller.getInstance().setHosts(hostList);
						addSubnetDialog.dispose();

					} else {
						new InfoDialog(Constants.ERROR, Constants.SIZEERROR);
					}
					}
			} else {
				new InfoDialog(Constants.ERROR, Constants.SIZEORSUFFIXERROR);
			}
		}
	}

	private Subnet checkFields(IPAddress address, String s) {

			String hostText = subnetBoxPanel.getSizeField().getText();
			String sufx = subnetBoxPanel.getSuffixField().getText();

			try {

				int hosts = Integer.parseInt(hostText);
				int suffix = Integer.parseInt(sufx);
				int hostClass = Calculator.getInstance().calculateIpClass(address.getAddressBits(0).getBitValue());
						
			if ((hosts <= Calculator.getInstance().calculatePossibleHosts(
					hostClass, address))) {

					if (suffix < 31
							&& suffix >= Calculator.getInstance()
									.getSuffixByAddress(address)) {

					return new Subnet(address, hosts, s);

					} else {
						new InfoDialog(Constants.ERROR, Constants.SUFFIXERROR);
					return null;
					}

				} else {
					new InfoDialog(Constants.ERROR, Constants.HOSTSIZEERROR);
				return null;
				}
			} catch (NumberFormatException ec) {
				new InfoDialog(Constants.ERROR, Constants.PARSEERRORHOST);
			return null;
			}

	}

}
